package org.example.common.base.response;

import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Result<T> {

    @ApiModelProperty(value = "状态码", example = "200")
    private int code;

    @ApiModelProperty(value = "是否成功", example = "true")
    private Boolean success;

    @ApiModelProperty(value = "消息描述")
    private String msg;

    @ApiModelProperty(value = "数据")
    private T data;

    @ApiModelProperty(value = "扩展数据")
    private ExtData extData;

    public static final int SUCCESS_CODE = 200;
    public static final int FAIL_CODE = 500;
    public static final String SUCCESS_MSG = "响应成功";

    public Result() {
    }

    public Result(Boolean success, int code, String msg, T data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(Boolean success, int code, String msg, T data, ExtData extData) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.extData = extData;
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(true, SUCCESS_CODE, SUCCESS_MSG, data);
    }

    public static <T> Result<T> success(T data, ExtData extData) {
        return new Result<>(true, SUCCESS_CODE, SUCCESS_MSG, data, extData);
    }

    public static <T> Result<T> error(int errorCode, String errorMsg) {
        return new Result<>(false, errorCode, errorMsg, null);
    }

    public static <T> Result<T> error(String errorMsg) {
        return new Result<>(false, 500, errorMsg, null);
    }
}
